package main

import (
	"github.com/jinzhu/gorm"
)
import 	_ "github.com/go-sql-driver/mysql"

/*
   Author: mahaiyuan
   Date: 2021/2/24 7:38 下午
   Description:
*/
func main() {
	db, err := gorm.Open("mysql", "root:12345678@tcp(localhost:3306)/gogogo?charset=utf8mb4&parseTime=True&loc=Local")
	db.LogMode(true)	//开启日志 打印执行的sql
	db.SingularTable(true)
	if err != nil {
		panic("fail to connect db")
	}
	defer db.Close()

	// where条件要在update之前
	//UPDATE `teacher` SET `birthday` = '2021-02-24 19:49:44', `work_age` = 20  WHERE (id = 7)
	//db.Model(&model.Teacher{}).Where("id = ?", 7).
	//	Updates(model.Teacher{Birthday: time.Now(), WorkAge: 20})
	//UPDATE `teacher` SET `birthday` = '2021-02-24 19:53:03', `work_age` = 2  WHERE (job_number = '20210020005')
	//db.Model(&model.Teacher{}).Where("job_number = ?", "20210020005").
	//	Updates(map[string]interface{}{"work_age": 2, "birthday": time.Now()})


	// 原生SQL
	//update teacher set work_age = 100 where id in (1,3,5)
	db.Exec("update teacher set work_age = ? where id in (?)", 100, []int64{1, 3, 5})
}
